<?php
/**
 * <i>@radios</i>组件
 * 
 * 生成一组radio，并保留节点的 x-name, x-class, x-style, x-size, x-title, x-accesskey 属性
 * 
 * 可以使用的参数有：
 * - x-options 选项数组
 * - [x-checked-values] array/scalar 选中的选项
 * - [x-checked-texts]  array/scalar 选中的文本  
 * - [x-separator]  string 分隔符
 * 
 * 
 * 示例：<br>
 *  模板：<br>
 * <code>
 *   <span x-name="myradio" com="@radios" x-options="$students" x-checked-values="$studentsChecked" x-checked-texts="$checkedTexts" separator=" |" />
 * </code>
 *  程序：
 * <code>
 *   $dataSet = array();
 *	 $dataSet["students"] = array("a", "b", "c", "d", "e", "f", "f", "g");
 *	 $dataSet["studentsChecked"] = array(2, 3, 5, 6);
 *	 $dataSet["checkedTexts"] = "d";
 *	 return new IModelAndTemplate($dataSet, "test");
 * </code>
 * 
 * @version $Id: IMintRadiosComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 */
class IMintRadiosComponent extends IAbstractMintComponent {
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 */	
	public function getSupportedParameters() {
		$array = array("x-options", "x-checked-values", "x-checked-texts", "x-separator");
		return $array;
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.0.3
	 */	
	public function getHtmlAttrMapping() {
		$array = array("x-name"=>"name", "x-class"=>"class", "x-style"=>"style", "x-size"=>"size", "x-title"=>"title", "x-accesskey"=>"accesskey");
		IMintComponentHelper::addHtmlEventAttrs($array);
		return $array;
	}
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 */	
	public function processComponent() {
		$parameters = $this->getParameters();
		if (!isset($parameters["x-options"])) {
			return false;
		}
		
		//node attributes
		$keepAttribute = $this->getKeepAttrs();
		
		//x-separator
		if (isset($parameters["x-separator"])) {
			$xSeparator = IMintVariable::getDefinition($parameters["x-separator"]);
			if ($xSeparator == null) {
				$xSeparator = $parameters["x-separator"];
			}
		}
		else {
			$xSeparator = " ";
		}
		
		//选项
		$xOptionsVar = IMintVariable::getDefinition($parameters["x-options"]);
		if ($xOptionsVar == null) {
			$this->throwVariableException("x-options");
		}
		$text = "<?php \$___radiosArray=array();";
		$text .= "foreach(" . $xOptionsVar . " as \$optionValue=>\$optionText) {";
		if (isset($parameters["x-checked-values"])) {
			$selectedValues = $parameters["x-checked-values"];
			$xValueVar = IMintVariable::getDefinition($selectedValues);
			if ($xValueVar == null) {
				$this->throwVariableException("x-checked-values");
			}
			$text .= "if (!is_array({$xValueVar}) && {$xValueVar} != \"\") {
				{$xValueVar} = array({$xValueVar});
			 }";
		}
		
		//选定的文本
		if (isset($parameters["x-checked-texts"])) {
			$selectTexts = $parameters["x-checked-texts"];
			$xTextVar = IMintVariable::getDefinition($selectTexts);
			if ($xTextVar == null) {
				$this->throwVariableException("x-checked-texts");
			}
			$text .= "if (!is_array({$xTextVar}) && {$xTextVar} != \"\") {
				{$xTextVar} = array({$xTextVar});
			 }";
		}
		
		//值变量
		if (isset($xValueVar)) {
			if (isset($xTextVar)) {
				 $text .= "if (in_array(\$optionValue, $xValueVar) || in_array(\$optionText, $xTextVar)) {\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" {$keepAttribute} checked=\\\"checked\\\" /> \" . \$optionText;}
				 else {
					\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" {$keepAttribute}> \" . \$optionText;
				 }";
			}
			else {
				 $text .= "if (in_array(\$optionValue, $xValueVar)) {\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" {$keepAttribute} checked=\\\"checked\\\"> \" . \$optionText;}
				 else {
					\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" {$keepAttribute}> \" . \$optionText;
				 }";
			}
		}
		else {
			if (isset($xTextVar)) {
				 $text .= "if (in_array(\$optionText, $xTextVar)) {\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" checked=\\\"checked\\\" {$keepAttribute}> \" . \$optionText;}
				 else {
					\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" {$keepAttribute}> \" . \$optionText;
				 }";	
			}
			else {
				$text .= "\$___radiosArray[]=\"<input type=\\\"radio\\\" value=\\\"\" . htmlspecialchars(\$optionValue) . \"\\\" {$keepAttribute}> \" . \$optionText;";
			}
		}
		$text .= "}\n";
		$text .= "echo implode(\"" . addslashes($xSeparator) . "\",\$___radiosArray);";
		$text .= " ?>";
		$this->addFirstChildScript($text);
		return true;
	}
}

?>